package com.android.newsreader;

import java.util.List;

import android.content.Context;

/**
* Class for persisting RSSItem objects to db4o db
*/
public class RSSItemProvider extends Db4oHelper {
	
	private static RSSItemProvider mProvider = null;

    public RSSItemProvider(Context ctx) {
                  super(ctx);
    }

    public static RSSItemProvider getInstance(Context ctx) {
                  if (mProvider == null)
                                mProvider = new RSSItemProvider(ctx);
                  return mProvider;
    }

    public void store(RSSItem rssitem) {
                  db().store(rssitem);
    }

    /**
     * Method for deleting RSSItem from database
     * @param rssitem item to be deleted
     * @throws Exception
     */
    public void delete(RSSItem rssitem) throws Exception {
    	List itemsToDelete = find(rssitem);
    	
    	if (itemsToDelete.size() > 0)
            db().delete(itemsToDelete.get(0));
    	else
    		throw new Exception();
    }

    /**
     * Retrieves all RSSItems stored on db
     * @return a list of retrieved RSSItems
     */
    public List findAll() {
           return db().query(RSSItem.class);
    }
    
    /**
     * Retrieves all RSSItems from db similar
     * to provided RSSItem
     * @param rssitem template RSSItem
     * @return list of retrieved items
     */
    public List find(RSSItem rssitem) {
        return db().queryByExample(rssitem);
    }
}
